﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;

namespace Wild2.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index(Int32 id)
        {
            Wild2.Models.Pages.User.Index page = new Models.Pages.User.Index();
            int onlinetime = 15;
            if (ConfigurationManager.AppSettings["onlinetime"] != null) { onlinetime = Int32.Parse(ConfigurationManager.AppSettings["onlinetime"]); }
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            page.UserData = Wild2.Models.User.User.GetUserData(id);
            if (UserID.HasValue)
            {
                Models.Favorites.Favorites.AddFavoriteToSession("Пользователь " + page.UserData.Nick);
                page.isSubscribed = Wild2.Models.User.Subscription.isSubscribed(UserID.Value, id);
            }
            if (Request.QueryString["votes"] != null)
            {
                page.Votes = Wild2.Models.User.User.GetVotesList(id);
            }
            page.isOnline = page.UserData.LastOnlineDate.AddMinutes(onlinetime).CompareTo(DateTime.Now) > 0;
            page.isAvatar = System.IO.File.Exists(Request.MapPath("~/Content/avatars/" + id + ".png"));
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(Int32 id, string subscr)
        {
            if (!String.IsNullOrEmpty(subscr))
            {
                Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
                if (UserID.HasValue)
                {
                    bool subscribed = Wild2.Models.User.Subscription.isSubscribed(UserID.Value, id);
                    if (subscribed)
                    {
                        Wild2.Models.User.Subscription.Unsubscribe(UserID.Value, id);
                    }
                    else
                    {
                        Wild2.Models.User.Subscription.Subscribe(UserID.Value, id);
                    }
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult Vote(int user, int vote, String hash)
        {
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                if (user != UserID.Value)
                {
                    if (hash == Wild2.Models.Auth.AuthOnSite.GetSessionHash())
                    {
                        if (vote < 0) { Wild2.Models.User.User.VoteUserMinus(UserID.Value, user); }
                        if (vote > 0) { Wild2.Models.User.User.VoteUserPlus(UserID.Value, user); }
                    }
                }
            }
            return RedirectToAction("Index", new { id = user });
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Top(int p, string nick)
        {
            Wild2.Models.Pages.User.Top page = new Models.Pages.User.Top();
            if (string.IsNullOrEmpty(nick)) page.UsersData = Wild2.Models.User.User.GetUsersData(p);
            else page.UsersData = Wild2.Models.User.User.GetUsersData(p, nick);
            if(string.IsNullOrEmpty(nick)) page.CountUsers = Wild2.Models.User.User.GetCountUsers();
            else page.CountUsers = Wild2.Models.User.User.GetCountUsers(nick);
            page.CurrentPage = p;
            page.LastPage = 1;
            page.ElOnPage = 15;
            if (ConfigurationManager.AppSettings["elonpage"] != null) { page.ElOnPage = Int32.Parse(ConfigurationManager.AppSettings["elonpage"]) * 3; }
            if (page.CountUsers % page.ElOnPage == 0) page.LastPage = page.CountUsers / page.ElOnPage;
            else { page.LastPage = (int)Math.Truncate((float)page.CountUsers / (float)page.ElOnPage) + 1; }
            if (page.CurrentPage > page.LastPage && page.LastPage > 0)
            {
                return RedirectToAction("Top", new { p = 1 });
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Top(int p, string nick, string usernick)
        {
            return RedirectToAction("Top", new { p = 1, nick = usernick });
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Subscr(Int32 id)
        {
            Wild2.Models.Pages.User.Subscr page = new Models.Pages.User.Subscr();
            if (id >= 0) page.UsersData = Wild2.Models.User.User.GetSubscribersDataToHim(Math.Abs(id));
            else page.UsersData = Wild2.Models.User.User.GetSubscribersDataHe(Math.Abs(id));
            return View(page);
        }
    }
}
